#include "LineEditProductClass.h"

#include <QLabel>
#include "qlineedit.h"
#include "qboxlayout.h"
#include "qregexp.h"
#include <QValidator>
#include <QEvent>
#include <QKeyEvent>

LineEditProductClass::LineEditProductClass(QWidget *parent) : QWidget(parent)
{
    bgColor = "#FFFFFF";
    borderColor = "#A6B5B8";
    borderRadius = 3;

    //用于显示下划线的标签,居中对齐
    labUnderline = new QLabel;
    labUnderline->setAlignment(Qt::AlignCenter);
    labUnderline->setText("_");

    //用于输入物料编号的文本框,居中对齐
    leMaterialNo = new QLineEdit;
    leMaterialNo->setObjectName("leMaterialNo");
    leMaterialNo->setAlignment(Qt::AlignCenter);
    leMaterialNo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    leMaterialNo->setPlaceholderText("物料编号");
    connect(leMaterialNo, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    //用于输入工序编号的文本框,居中对齐
    leWorkProcedureSequence = new QLineEdit;
    leWorkProcedureSequence->setObjectName("leWorkProcedureSequence");
    leWorkProcedureSequence->setAlignment(Qt::AlignCenter);
    leWorkProcedureSequence->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    leWorkProcedureSequence->setPlaceholderText("工序编号");
    connect(leWorkProcedureSequence, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    //设置IP地址校验过滤
    QString pattern = "[A-Za-z0-9]+";
    QRegExp regExp(pattern);
    QRegExpValidator *validator = new QRegExpValidator(regExp, this);

    leMaterialNo->setValidator(validator);
    leWorkProcedureSequence->setValidator(validator);

    //绑定事件过滤器,识别键盘按下
    leMaterialNo->installEventFilter(this);
    leWorkProcedureSequence->installEventFilter(this);


    QFrame *frame = new QFrame;
    frame->setObjectName("frameProductClass");

    QStringList qss;
    qss.append("QWidget { font-family: Arial; font-size: 18px; }"); // 添加这行来设置全局字体
    qss.append(QString("QFrame#frameIP{border:1px solid %1;border-radius:%2px;}").arg(borderColor).arg(borderRadius));
    qss.append(QString("QLabel{min-width:15px;background-color:%1;}").arg(bgColor));
    qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
    qss.append(QString("QLineEdit#leMaterialNo{border-top-left-radius:%1px;border-bottom-left-radius:%1px;}").arg(borderRadius));
    qss.append(QString("QLineEdit#leWorkProcedureSequence{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}").arg(borderRadius));
    frame->setStyleSheet(qss.join(""));


    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    verticalLayout->setSpacing(0);
    verticalLayout->addWidget(frame);

    //将控件按照横向布局排列
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(leMaterialNo);
    layout->addWidget(labUnderline);
    layout->addWidget(leWorkProcedureSequence);
}

QString LineEditProductClass::getProductClass() const
{
    return this->productClass;

}

QSize LineEditProductClass::sizeHint() const
{
    return QSize(250, 20);
}

QSize LineEditProductClass::minimumSizeHint() const
{
    return QSize(30, 10);
}

void LineEditProductClass::setProductClass(const QString &productClass)
{
    //先检测IP地址是否合法
    QRegExp regExp("^[A-Za-z0-9]+_[A-Za-z0-9]+$");
    if (!regExp.exactMatch(productClass)) {
        return;
    }

    if (this->productClass != productClass) {
        this->productClass = productClass;

        //将IP地址填入各个网段
        QStringList list = productClass.split(labUnderline->text());
        leMaterialNo->setText(list.at(0));
        leWorkProcedureSequence->setText(list.at(1));
    }
}

void LineEditProductClass::clear()
{
    leMaterialNo->clear();
    leWorkProcedureSequence->clear();
    leMaterialNo->setFocus();
}

void LineEditProductClass::setBgColor(const QString &bgColor)
{
    if (this->bgColor != bgColor) {
        this->bgColor = bgColor;
    }
}

void LineEditProductClass::setBorderColor(const QString &borderColor)
{
    if (this->borderColor != borderColor) {
        this->borderColor = borderColor;
    }
}

void LineEditProductClass::setBorderRadius(int borderRadius)
{
    if (this->borderRadius != borderRadius) {
        this->borderRadius = borderRadius;
    }
}

bool LineEditProductClass::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QLineEdit *txt = (QLineEdit *)watched;
        if (txt == leMaterialNo || txt == leWorkProcedureSequence ) {
            QKeyEvent *key = (QKeyEvent *)event;

            //如果当前按下了下划线则移动焦点到下一个输入框
            if (key->text() == "_") {
                this->focusNextChild();
            }

            // 如果是第一个输入框，不移动焦点，只执行退格删除操作
            if (watched == leMaterialNo) {
                return false; // 继续执行默认的退格操作
            }

            //如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
            if (key->key() == Qt::Key_Backspace) {
                if (txt->text().length() <= 1) {
                    this->focusNextPrevChild(false);
                }
            }
        }
    }

    return QWidget::eventFilter(watched, event);
}

void LineEditProductClass::textChanged(const QString &text)
{
    //物料编号和工序编号，拼接成产品类别
    productClass = QString("%1_%2").arg(leMaterialNo->text()).arg(leWorkProcedureSequence->text());
}
